package com.item.service;

import com.base.support.PropertySupport;
import com.base.util.JPushUtil;
import com.base.util.StringUtil;
import com.item.SystemConst;
import com.item.dao.NotifyMapper;
import com.item.dao.UserMapper;
import com.item.dao.model.*;
import com.item.daoEx.NotifyMapperEx;
import com.item.daoEx.model.NotifyEx;
import com.item.SystemMessage;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class NotifyService {
    @Autowired
    private NotifyMapper notifyMapper;
    @Autowired
    private NotifyMapperEx notifyMapperEx;
    //@Autowired
    //private UserNotifyMapper userNotifyMapper;
    @Autowired
    private SmsSendLogService smsSendLogService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MessageService messageService;

    private static final Logger logger = Logger.getLogger(NotifyService.class);

    public int countByExample(NotifyExample example) {
        return this.notifyMapper.countByExample(example);
    }

    public Notify selectByPrimaryKey(Integer id) {
        return this.notifyMapper.selectByPrimaryKey(id);
    }

    public List<Notify> selectByExample(NotifyExample example) {
        return this.notifyMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Integer id) {
        return this.notifyMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Notify record) {
        return this.notifyMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Notify record) {
        return this.notifyMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(NotifyExample example) {
        return this.notifyMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Notify record, NotifyExample example) {
        return this.notifyMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Notify record, NotifyExample example) {
        return this.notifyMapper.updateByExample(record, example);
    }

    public int insert(Notify record) {
        return this.notifyMapper.insert(record);
    }

    public int insertSelective(Notify record) {
        return this.notifyMapper.insertSelective(record);
    }

	public List<NotifyEx> selectList(Map<String, Object> map) {
		return notifyMapperEx.selectList(map);
	}

//	public void deleteEx(Integer id) {
//		UserNotifyExample example = new UserNotifyExample();
//		example.createCriteria().andNidEqualTo(id);
//		userNotifyMapper.deleteByExample(example);
//		notifyMapper.deleteByPrimaryKey(id);
//	}

	public void sendDanmu(String nickName,String goodsName,Integer num){
        Notify notify = new Notify();
        SystemMessage mkmMessage = SystemMessage.GET_GOODS_MESSAGE;
        notify.setType(mkmMessage.getType());
        notify.setNum(0);
        notify.setState(2);
        notify.setContent(StringUtil.format(mkmMessage.getTemplate(),nickName,goodsName,num));
        notify.setTitle(mkmMessage.getTitle());
        notify.setRedirectType(mkmMessage.getRedirectType());
        notifyMapper.insertSelective(notify);
        //发送消息
        if(StringUtils.isBlank(PropertySupport.getProperty("jpush.appkey"))){
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("type", SystemConst.DANMU_UGET);
        params.put("msg", notify.getContent());
        params.put("id",notify.getId().toString());
        JPushUtil.pushOnlyMessageToApp(params,notify.getContent());
    }




    /**
     * 新注册用户
     * @param userId
     * @return
     */
//    public int insertNewUser(Integer userId){
//        NotifyExample example = new NotifyExample();
//        example.createCriteria().andUserIdIsNull().andStateEqualTo(2).andTypeEqualTo(1);
//        List<Notify> list = notifyMapper.selectByExample(example);
//        List<UserNotify> notifies = new ArrayList<>(list.size());
//        Date now = new Date();
//        for (Notify notify : list){
//            UserNotify record = getByNotify(notify);
//            record.setCreateTime(now);
//            record.setUserId(userId);
//            notifies.add(record);
//        }
//        if (notifies.size() > 0){
//            userNotifyMapperEx.insertBatch(notifies);
//        }
//
//        return list.size();
//    }
//

    /**
     * 个推
     * @param notify
     */
    public void sendMsgToUser(Notify notify){
        Notify record = notifyMapper.selectByPrimaryKey(notify.getId());
        if(record != null){
            if(null != record.getUserId()){
                messageService.pushToSingle(record.getUserId(), record.getContent(), 0,"");
            }
        }

    }

}